<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$DAYS = 14;

$today = date('Y-m-d', time());
$cond_vals = new stdClass();
$cond_vals->c = 'date = :v1 AND is_cancel = 0';
$cond_vals->v = array(':v1' => $today);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _total';
$today_orders = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
if (!$today_orders) {
  $today_orders = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'date = :v1 AND is_cancel = 0';
$cond_vals->v = array(':v1' => $today);
$opt = new stdClass();
$opt->select_expr = 'SUM(t.total) AS _total';
$today_income = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
if (!$today_income) {
  $today_income = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'date = :v1 AND is_paid = 0 AND is_cancel = 0';
$cond_vals->v = array(':v1' => $today);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _total';
$today_unpaid_orders = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
if (!$today_unpaid_orders) {
  $today_unpaid_orders = 0;
}

$today = date('Y-m-d', time());
$cond_vals = new stdClass();
$cond_vals->c = 'date = :v1 AND is_paid = 0 AND is_cancel = 0';
$cond_vals->v = array(':v1' => $today);
$opt = new stdClass();
$opt->select_expr = 'SUM(t.total) AS _total';
$today_unpaid_income = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
if (!$today_unpaid_income) {
  $today_unpaid_income = 0;
}

$orders = array();
$customers = array();
$max_orders = 0;
$max_customers = 0;
for ($i = strtotime($today), $il = ($i - ($DAYS * 86400)); $i > $il; $i-=86400) {
  $cond_vals = new stdClass();
  $cond_vals->c = 'date = :v1 AND is_paid = 1 AND is_cancel = 0';
  $cond_vals->v = array(':v1' => date('Y-m-d', $i));
  $opt = new stdClass();
  $opt->select_expr = 'SUM(t.total) AS _total';
  $total = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
  if (!$total) {
    $total = 0;
  }
  $max_orders = max($total, $max_orders);
  $orders[] = array(date('Y-m-d', $i), $total, "$$total");
  
  $cond_vals = new stdClass();
  $cond_vals->c = 'reg_date = :v1 AND is_actived > 0';
  $cond_vals->v = array(':v1' => date('Y-m-d', $i));
  $opt = new stdClass();
  $opt->select_expr = 'COUNT(t.id) AS _total';
  $total = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_total;
  if (!$total) {
    $total = 0;
  }
  $max_customers = max($total, $max_customers);
  $customers[] = array(date('Y-m-d', $i), $total, '' . $total);
}

$cond_vals = new stdClass();
$cond_vals->c = 'is_paid = :v1 AND is_delivered = :v2 AND is_cancel = 0';
$cond_vals->v = array(':v1' => 1, ':v2' => 0);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$paid = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$paid) {
  $paid = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'is_paid = :v1 AND is_delivered = :v2 AND is_cancel = 0';
$cond_vals->v = array(':v1' => 0, ':v2' => 0);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$unpaid = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$unpaid) {
  $unpaid = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'is_delivered = 1 AND date_deliver >= :v1 AND is_cancel = 0';
$cond_vals->v = array(':v1' => date('Y-m-d', time() - (864000)));
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$shipped = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$shipped) {
  $shipped = 0;
}

$cond_vals = new stdClass(); 
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 1);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$available = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$available) {
  $available = 0;
}

$cond_vals = new stdClass(); 
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 0);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$unavailable = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$unavailable) {
  $unavailable = 0;
}

$cond_vals = new stdClass(); 
$cond_vals->c = 't.alert__ = :v1';
$cond_vals->v = array(':v1' => 1);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$out_stock = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$out_stock) {
  $out_stock = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'is_actived = :v1';
$cond_vals->v = array(':v1' => 1);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$act = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$act) {
  $act = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'is_actived = :v1';
$cond_vals->v = array(':v1' => 0);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$inact = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$inact) {
  $inact = 0;
}

$cond_vals = new stdClass();
$cond_vals->c = 'is_actived = :v1';
$cond_vals->v = array(':v1' => 2);
$opt = new stdClass();
$opt->select_expr = 'COUNT(t.id) AS _count';
$suspend = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d[0]->_count;
if (!$suspend) {
  $suspend = 0;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #222;
  border-left: 1px solid #222;
  margin-top: 10px;
}
table.form td {
  padding: 3px 5px; 
  vertical-align: middle;
}
table.form th.header {
  text-align: left;
  color: #fff;
  background-color: #222;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}
table.form td.title {
  background-color: #666;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
  width: 200px;
  color: #fff;
  font-weight: bold;
}
table.form td.content {
  width: 100px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}
table.form a {
  color: #666;
}
.title {
  font-family: Lucida Sans Unicode;
  font-weight: bold;
}
.today {
  text-align: center;
  background: #fff;
  border: 1px solid #999;
  padding: 20px 0;
}
.today div.t_title {
  font-family: Lucida Sans Unicode;
  font-weight: bold;
  font-size: 20px;
}
.today div.t_order {
  font-family: Lucida Sans Unicode;
  font-weight: bold;
  font-size: 24px;
  color: #ff8000;
  margin: 5px 0;
}
.today div.t_income {
  font-family: Verdana;
  font-weight: bold;
  font-size: 16px;
  color: #e7806f;
}
.today div.t_unpaid {
  font-family: Lucida Sans Unicode;
  font-size: 13px;
  color: #6f6f6f;
}
.imgbtn {
  background-image: url(images/more.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 36px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
</style>
</head>
<body>
<table cellpadding=0 cellspacing=0>
  <tr>
    <td style="vertical-align: top;">
      <div class="title">
        <table cellpadding=0 cellspacing=0>
         <tr>
           <td style="width: 220px;">Payment Received Over Last 14 Days</td>
           <td><button class="imgbtn" onclick="go(<?php echo ADMIN_RPT_REV_ID; ?>, 1);" style="margin-left: 5px;">More details</button></td>
         </tr>
        </table>
      </div>
      <div id="chart1" style="height:200px; width:650px;"></div>
      <div>&nbsp;</div>
      <div class="title">
        <table cellpadding=0 cellspacing=0>
         <tr>
           <td style="width: 220px;">New Customers Over Last 14 Days</td>
           <td><button class="imgbtn" onclick="go(<?php echo ADMIN_RPT_CUS_ID; ?>, 1);" style="margin-left: 5px;">More details</button></td>
         </tr>
        </table>
      </div>
      <div id="chart2" style="height:200px; width:650px;"></div>
    </td>
    <td style="vertical-align: top;">
      <div class="today">
        <div class="t_title">Today's Order</div>
        <div class="t_order"><?php echo $today_orders; ?></div>
        <div class="t_income">$<?php echo $today_income; ?></div>
        <div>&nbsp;</div>
        <div class="t_unpaid"><?php echo $today_unpaid_orders; ?> Unpaid Orders ($<?php echo $today_unpaid_income; ?>)</div>
      </div>
      <table class="form" cellpadding=0 cellspacing=0>
        <tr><th class="header" colspan=2>Orders</th></tr>
        <tr>
          <td class="title">Requires Shipping</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_ORDER_ID; ?>, 2);"><?php echo $paid; ?></a></td>
        </tr>
        <tr>
          <td class="title">Unpaid Orders</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_ORDER_ID; ?>, 1);"><?php echo $unpaid; ?></a></td>
        </tr>
        <tr>
          <td class="title">Shipped in last 10 days</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_ORDER_ID; ?>, 3);"><?php echo $shipped; ?></a></td>
        </tr>
        <tr><th class="header" colspan=2>Products</th></tr>
        <tr>
          <td class="title">Available</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_PRODUCT_ID; ?>, 1);"><?php echo $available; ?></a></td>
        </tr>
        <tr>
          <td class="title">Unavailable</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_PRODUCT_ID; ?>, 2);"><?php echo $unavailable; ?></a></td>
        </tr>
        <tr>
          <td class="title">Out of Stock</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_PRODUCT_ID; ?>, 3);"><?php echo $out_stock; ?></a></td>
        </tr>
        <tr><th class="header" colspan=2>Customers</th></tr>
        <tr>
          <td class="title">Activated</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_CUSTOMER_ID; ?>, 2);"><?php echo $act; ?></a></td>
        </tr>
        <tr>
          <td class="title">Inactivated</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_CUSTOMER_ID; ?>, 1);"><?php echo $inact; ?></a></td>
        </tr>
        <tr>
          <td class="title">Suspended</td>
          <td class="content"><a href="#" onclick="go(<?php echo ADMIN_CUSTOMER_ID; ?>, 3);"><?php echo $suspend; ?></a></td>
        </tr>
      </table>
    </td>
  </tr>
</table>

<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>
<script type="text/javascript" src="../../../libs/jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="../../../libs/jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="../../../libs/jqplot/plugins/jqplot.dateAxisRenderer.min.js"></script>
<link rel="stylesheet" type="text/css" hrf="../../../libs/jqplot/jquery.jqplot.min.css" />
<script type="text/javascript" src="../../../libs/jqplot/plugins/jqplot.pointLabels.min.js"></script>
<script type="text/javascript">
var _orders = JSON.parse('<?php echo addslashes(json_encode($orders)); ?>');
var _customers = JSON.parse('<?php echo addslashes(json_encode($customers)); ?>');
$(document).ready(function () {
  var plot1 = $.jqplot('chart1', [_orders], {
    seriesDefaults: {
      rendererOptions: {
        smooth: true
      },
      showMarker:false,
      pointLabels: { show:true } 
    },
    axes:{
      xaxis:{
        renderer:$.jqplot.DateAxisRenderer,
        tickOptions:{formatString:'%#d %b'},
        tickInterval:'1 day'
      },
      yaxis: {
        min: 0,
        max: <?php echo $max_orders * 1.5; ?>,
        tickOptions: {
          prefix: '$',
          showLabel: false
        }
      }
    }
  });
  var plot2 = $.jqplot('chart2', [_customers], {
    seriesDefaults: {
      rendererOptions: {
        smooth: true
      },
      showMarker:false,
      pointLabels: { show:true } 
    },
    axes:{
      xaxis:{
        renderer:$.jqplot.DateAxisRenderer,
        tickOptions:{formatString:'%#d %b'},
        tickInterval:'1 day'
      },
      yaxis: {
        min: 0,
        max: <?php echo max($max_customers * 2, 4); ?>,
        tickOptions: {
          showLabel: false
        }
      }
    }
  });
});

function go(page_id, cat) {
  window.open('http://<?php echo URL . 'admin.php?i='; ?>' + page_id + '&cat=' + cat);
}
</script>
</body>
</html>